#[macro_use]
extern crate rbatis;

pub mod init;

use crate::init::init_db;
use log::LevelFilter;
use rbatis::rbdc::types::json::JsonV;
use rbatis::table_sync;
use rbatis::table_sync::SqliteTableMapper;
use rbs::to_value;

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Account {
    pub id: Option<String>,
    pub name: Option<String>,
}

#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct User {
    pub id: Option<String>,
    //json column
    pub account: JsonV<Account>,
}
crud!(User {});

#[tokio::main]
pub async fn main() {
    fast_log::init(
        fast_log::Config::new()
            .console()
            .level(log::LevelFilter::Debug),
    )
    .expect("rbatis init fail");
    let rb = init_db().await;

    let user = User {
        id: Some("1".to_string()),
        account: JsonV(Account {
            id: Some("2".to_string()),
            name: Some("xxx".to_string()),
        }),
    };
    fast_log::LOGGER.set_level(LevelFilter::Off);
    _ = table_sync::sync(&rb.acquire().await.unwrap(), &SqliteTableMapper{},to_value!(&user), "user")
        .await;
    fast_log::LOGGER.set_level(LevelFilter::Info);

    let v = User::insert(&rb.clone(), &user).await;
    println!("insert:{:?}", v);

    let users = User::select_by_column(&rb.clone(), "id", "1").await;
    println!("select:{}", to_value!(users));
}
